<?php
include("./include/config.inc.php");
if(!isset($_SESSION['s_activId']))
{
  $_SESSION['s_urlRedirectDir'] = $_SERVER['REQUEST_URI'];
	header("Location:checkLogin.php");
}
$today = date('Y-m-d');
$chkInterestToday = "SELECT loanId, pendingAmount, monthlyInterestRs
                       FROM loan 
                      WHERE loanDate ='".date ("Y-m-d", strtotime("-30 day", strtotime($today)))."'";
$chkInterestTodayRes = mysql_query($chkInterestToday);
$chkInterestTodayNumRows = mysql_num_rows($chkInterestTodayRes);
if($chkInterestTodayNumRows > 0)
{
	while($chkInterestTodayRow = mysql_fetch_array($chkInterestTodayRes))
	{
    $chkLoneavailableRecord = "SELECT `loanId` 
                                 FROM `interestreceived`
                                WHERE `receivedDate` = '".$today."'
                                  AND `loanId` = ".$chkInterestTodayRow['loanId'];
    $chkLoneavailableRecordRes = mysql_query($chkLoneavailableRecord);
    $chkLoneavailableRecordNumRows = mysql_num_rows($chkLoneavailableRecordRes);
    if($chkLoneavailableRecordNumRows == 0)
    {
    	$nextMonth = date('Y-m-d',strtotime('+30 day',strtotime($i)));
      $insertInterestRecived = "INSERT INTO `interestreceived` (`loanId`, `pendingAmount`, `interest`, `receivedDate`, `isReceive`)
                                     VALUES ('".$chkInterestTodayRow['loanId']."','".$chkInterestTodayRow['pendingAmount']."','".$chkInterestTodayRow['monthlyInterestRs']."','".$today."',0)";
      $insertInterestRecivedRes = mysql_query($insertInterestRecived);
      $insertInterestRecived2 = "INSERT INTO `interestreceived` (`loanId`, `pendingAmount`, `interest`, `receivedDate`, `isReceive`)
                                     VALUES ('".$chkInterestTodayRow['loanId']."','".$chkInterestTodayRow['pendingAmount']."','".$chkInterestTodayRow['monthlyInterestRs']."','".$nextMonth."',0)";
      $insertInterestRecived2Res = mysql_query($insertInterestRecived2);
    }
  }
}
include("./bottom.php");
$smarty->display('index.tpl');
?>